package com.yzt.zhuju.network.biz;

import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.R;
import com.yzt.zhuju.arouter.RouterUser;
import com.yzt.zhuju.network.NetLayer;
import com.yzt.zhuju.network.apiconfig.AccountUrlConstant;
import com.yzt.zhuju.network.apiconfig.DynamicUrlConstant;
import com.yzt.zhuju.network.entity.login.GetSMSCodeRequest;
import com.yzt.zhuju.network.entity.login.LoginOrRegisterRequest;
import com.yzt.zhuju.network.entity.user.FollowUserStatusRequest;
import com.yzt.zhuju.utils.RegexUtils;
import com.yzt.p_base.AppUtils;
import com.yzt.p_base.utils.MD5Util;
import com.yzt.p_base.utils.ToastUtil;

import java.util.List;
import java.util.Map;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Observer;

/**
 * author : bob
 * date   : 2022/12/6
 * desc   :
 */
public class AccountModule extends BaseModule {

    /**
     * 登录
     */
    public void postLogin(LoginOrRegisterRequest mLoginOrRegisterRequest, Observer observer) {
        if (!RegexUtils.isMatch("[0-9]+", mLoginOrRegisterRequest.getMobile())) {
            ToastUtil.showMessage(AppUtils.getContext().getString(R.string.please_fill_in_the_correct_mobile_phone_number));
            return;
        }
        JSONObject json = new JSONObject();
        json.put("userName", mLoginOrRegisterRequest.getMobile());
        json.put("password", MD5Util.md5(mLoginOrRegisterRequest.getPassWord()));
        Observable observable = NetLayer.getInstance().getAccountService().postLogin(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.POST_PWD_LOGIN);
    }

    /**
     * 手机号登录
     */
    public void phoneLogin(String phone, String msg, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("userName", phone);
        json.put("code", msg);
        Observable observable = NetLayer.getInstance().getAccountService().phoneLogin(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.POST_PHONE_LOGIN);
    }

    /**
     * 获取融云IMToken
     */
    public void getIMToken(Observer observer) {
        JSONObject json = new JSONObject();
        Observable observable = NetLayer.getInstance().getAccountService().getRongInfo(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.GET_RONG_INFO);
    }

    /**
     * 第三方登录
     */
    public void otherLogin(String code, String phone, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("code", code);
        json.put("phone", phone);
        Observable observable = NetLayer.getInstance().getAccountService().otherLogin(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.POST_OTHER_LOGIN);
    }

    /**
     * 一键登录
     */
    public void oneKeyLogin(String token, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("token", token);
        Observable observable = NetLayer.getInstance().getAccountService().oneKeyLogin(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.POST_ONE_KEY_LOGIN);
    }

    /**
     * 选择国家/地区
     */
    public void getCountryOrZoneCode(Observer observer) {
        Observable observable = NetLayer.getInstance().getAccountService().getCountryOrZoneCode();
        toSubscribe2Filter(observable, observer, AccountUrlConstant.GET_COUNTRY_OR_ZONE_CODE);
    }

    /**
     * 获取验证码
     */
    public void getVerifyCode(GetSMSCodeRequest mGetSMSCodeRequest, Observer observer) {
        //1-登录注册 2-找回密码 3-修改密码 4-修改手机号 5-修改转接号码
        if (!RegexUtils.isMatch("[0-9]+", mGetSMSCodeRequest.getPhone())) {
            ToastUtil.showMessage(AppUtils.getContext().getString(R.string.please_fill_in_the_correct_mobile_phone_number));
            return;
        }
        RequestBody mRequestBody = getRequestBody(mGetSMSCodeRequest);
        Observable observable = NetLayer.getInstance().getAccountService().postGetVerifyCode(mRequestBody);
        toSubscribe2Filter(observable, observer, AccountUrlConstant.GET_VERIFY_CODE);
    }


    /**
     * 获取登录的验证码
     */
    public void getLoginCode(String phone, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("phone", phone);
        Observable observable = NetLayer.getInstance().getAccountService().postLoginVerifyCode(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.GET_LOGIN_VERIFY_CODE);
    }

    /**
     * 获取修改手机号验证码
     */
    public void getAccountPhoneUpdateCode(String phone, Observer observer) {
        JSONObject json = new JSONObject();
        // json.put("phone", phone);
        Observable observable = NetLayer.getInstance().getAccountService().getAccountPhoneUpdateCode(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.GET_ACCOUNT_PHONE_UPDATE_CODE);
    }

    /**
     * 获取修改手机号验证码
     */
    public void accountPhoneUpdate(String phone, String code, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("phone", phone);
        json.put("code", code);
        Observable observable = NetLayer.getInstance().getAccountService().accountPhoneUpdate(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.GET_ACCOUNT_PHONE_UPDATE);
    }

    /**
     * 修改密码
     */
    public void modifyPwd(String phone, String cookie, String passWord, Observer observer) {
        if (!RegexUtils.isMatch("[0-9]+", phone)) {
            ToastUtil.showMessage(AppUtils.getContext().getString(R.string.please_fill_in_the_correct_mobile_phone_number));
            return;
        }
        JSONObject json = new JSONObject();
        json.put("cookie", cookie);
        json.put("passWord", MD5Util.md5(passWord));
        json.put("phone", phone);
        Observable observable = NetLayer.getInstance().getAccountService().modifyPwd(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_USER_MODIFY_PWD);
    }

    /**
     * 黑名单
     */
    public void blackList(int page, int size, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("size", size);
        json.put("page", page);
        Observable observable = NetLayer.getInstance().getAccountService().blackList(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_USER_BLACKLIST);
    }

    /**
     * 好友列表
     */
    public void friendList(int type, String keyword, String userId, int page, int size, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("current", size);
        json.put("pageSize", page);
        json.put("userId", userId);
        if (!keyword.equals("")) {
            json.put("keyword", keyword);
        }

        Observable observable;
        switch (type) {
            case RouterUser.FriendType.TYPE_FRIEND:
                //好友列表
                observable = NetLayer.getInstance().getAccountService().goodFriendList(getRequestBody(json));
                toSubscribe2Filter(observable, observer, AccountUrlConstant.API_USER_GOOD_FRIEND_LIST);
                break;
            case RouterUser.FriendType.TYPE_FOLLOW:
                //关注列表
                observable = NetLayer.getInstance().getAccountService().followList(getRequestBody(json));
                toSubscribe2Filter(observable, observer, AccountUrlConstant.API_USER_FOLLOW_LIST);
                break;
            case RouterUser.FriendType.TYPE_FANS:
                //粉丝列表
                observable = NetLayer.getInstance().getAccountService().fansList(getRequestBody(json));
                toSubscribe2Filter(observable, observer, AccountUrlConstant.API_USER_FANS_LIST);
                break;
            default:
                break;
        }
    }

    /**
     * 好友列表
     */
    public void friendList(String keyword, String userId, int page, int size, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("current", size);
        json.put("pageSize", page);
        json.put("userId", userId);
        if (!keyword.equals("")) {
            json.put("keyword", keyword);
        }
        Observable observable = NetLayer.getInstance().getAccountService().goodFriendList(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_USER_GOOD_FRIEND_LIST);
    }

    /**
     * 拉黑/取消拉黑
     */
    public void cancelBlack(String buid, int status, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("buid", buid);
        json.put("status", status);
        Observable observable = NetLayer.getInstance().getAccountService().cancelBlack(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_USER_CANCEL_BLACK);
    }

    /**
     * 获取用户完整信息
     */
    public void getUserFullInfo(String targetUid, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("userId", targetUid);
        Observable observable = NetLayer.getInstance().getAccountService().getUserFullInfo(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_MINE_GET_OTHER_INFO);
    }

    /**
     * 删除视频
     */
    public void delVideo(Observer observer) {
        JSONObject json = new JSONObject();
        Observable observable = NetLayer.getInstance().getAccountService().delVideo(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_MINE_EDIT_DEL_VIDEO);
    }

    /**
     * 按需获取用户信息的部分
     */
    public void getFullInfoParts(Map<String, String> options, Observer observer) {
        Observable observable = NetLayer.getInstance().getAccountService().getFullInfoParts(getRequestBody(options));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_MINE_GET_FULL_INFO_PARTS);
    }

    /**
     * 绑定推送
     */
    public void bindPush(Map<String, String> options, Observer observer) {
        Observable observable = NetLayer.getInstance().getAccountService().bindPush(getRequestBody(options));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_BIND_PUSH);
    }

    /**
     * 用户资料完善
     */
    public void editUserInfo(JSONObject json, Observer observer) {
        Observable observable = NetLayer.getInstance().getAccountService().editUserInfo(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_MINE_EDIT_USER_INFO);
    }

    /**
     * 用户资料完善
     */
    public void registerEditUserInfo(JSONObject json, Observer observer) {
        Observable observable = NetLayer.getInstance().getAccountService().registerEditUserInfo(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_MINE_EDIT_USER_INFO_REGISTER);
    }

    /**
     * 用户资料完善
     */
    public void editUserInfo(String key, String value, Observer observer) {
        JSONObject json = new JSONObject();
        json.put(key, value);
        editUserInfo(json, observer);
    }

    /**
     * 关注/取消关注
     */
    public void postFollowOrUnfollowUser(FollowUserStatusRequest mFollowUserStatusRequest, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("userId", mFollowUserStatusRequest.getUidRels().get(0));
        if (mFollowUserStatusRequest.getStatus() == 1) {
            Observable observable = NetLayer.getInstance().getAccountService().postFollowOrfollowUser(getRequestBody(json));
            toSubscribe2Filter(observable, observer, AccountUrlConstant.POST_FOLLOW_OR_FOLLOW_USER);
        } else {
            Observable observable = NetLayer.getInstance().getAccountService().postFollowOrUnfollowUser(getRequestBody(json));
            toSubscribe2Filter(observable, observer, AccountUrlConstant.POST_FOLLOW_OR_UN_FOLLOW_USER);
        }
    }

    /**
     * 获取用户基本信息
     */
    public void getBasicUserInfoRequest(String targetUid, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("userId", targetUid);
        Observable observable = NetLayer.getInstance().getAccountService().getOtherInfo(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_MINE_GET_OTHER_INFO);
    }

    /**
     * 批量获取用户信息
     */
    public void loadUserProfileList(Object[] idsList, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("userIds", idsList);
        json.put("current", 1);
        json.put("pageSize", 999);
        Observable observable = NetLayer.getInstance().getAccountService().loadUserProfileList(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.GET_USER_LOAD_USER_PROFILE_LIST);
    }

    /**
     * 批量获取用户信息
     */
    public void loadGroupProfileList(List<String> idsList, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("rongGroupIds", idsList);
        Observable observable = NetLayer.getInstance().getAccountService().loadGroupProfileList(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.GET_GROUP_LOAD_GROUP_PROFILE_LIST);
    }

    /**
     * 举报
     */
    public void relationInform(String dynamicId, String targetUid, int reportType, String reason, List<String> list, Observer observer) {
        JSONObject json = new JSONObject();
        if (dynamicId != null) {
            json.put("dynId", dynamicId);
        }
        json.put("targetUserId", targetUid);
        json.put("reportType", reportType);
        json.put("remark", reason);
        json.put("reportPicList", list);


        if (dynamicId != null) {
            Observable observable = NetLayer.getInstance().getDynamicService().dynamicReport(getRequestBody(json));
            toSubscribe2Filter(observable, observer, DynamicUrlConstant.GET_DYNAMIC_REPORT);
        } else {
            Observable observable = NetLayer.getInstance().getAccountService().relationInform(getRequestBody(json));
            toSubscribe2Filter(observable, observer, AccountUrlConstant.API_HEAD_GET_CONFIG_URE);
        }
    }

    /**
     * 获取礼物墙列表
     */
    public void loadGiftWallList(String targetUid, int page, int size, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("tagUid", targetUid);
        json.put("pageNum", page);
        json.put("pageSize", size);
        Observable observable = NetLayer.getInstance().getAccountService().loadGiftWallList(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_USER_GIFT_WALL_LIST);
    }

    /**
     * 申请注销
     */
    public void logoff(String canUid, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("canUid", canUid);
        Observable observable = NetLayer.getInstance().getAccountService().logoff(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_USER_LOGOFF);
    }

    /**
     * 搜索用户
     */
    public void searchUser(String searchKey, int page, int size, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("searchKey", searchKey);
        json.put("page", page);
        json.put("isShine", 0);
        json.put("size", size);
        Observable observable = NetLayer.getInstance().getAccountService().searchUser(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_SEARCH_USER);
    }

    /**
     * 上传文件
     *
     * @param part 需要上传的文件
     */
    public void uploadFile(MultipartBody.Part part, int gender, Observer observer) {
        Observable observable = NetLayer.getInstance().getAccountService().uploadFile(part, gender);
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_MINE_SUBMIT_FILE);
    }

    /**
     * 上传文件
     *
     * @param part 需要上传的文件
     */
    public void uploadFileLite(MultipartBody.Part part, Observer observer) {
        Observable observable = NetLayer.getInstance().getAccountService().uploadFileLite(part);
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_MINE_SUBMIT_IMAGE_LITE);
    }

    /**
     * 上传视频
     */
    public void uploadFileVideo(MultipartBody.Part part, Observer observer) {
        Observable observable = NetLayer.getInstance().getAccountService().uploadFileVideo(part);
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_MINE_SUBMIT_FILE);
    }

    /**
     * 上传视频
     */
    public void uploadFileVideoLite(MultipartBody.Part part, Observer observer) {
        Observable observable = NetLayer.getInstance().getAccountService().uploadFileVideoLite(part);
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_MINE_SUBMIT_FILE_VIDEO_LITE);
    }

    /**
     * 点击签到
     */
    public void touchSign(Observer observer) {
        JSONObject json = new JSONObject();
        Observable observable = NetLayer.getInstance().getAccountService().touchSign(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_USER_SIGN);
    }

    /**
     * 修改提醒状态
     */
    public void modifySignTip(int status, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("status", status);
        Observable observable = NetLayer.getInstance().getAccountService().modifySignTip(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_USER_SIGN_TIP_STATUS);
    }

    /**
     * 获取激励视频基础信息
     */
    public void getWatchVideoInfo(Observer observer) {
        Observable observable = NetLayer.getInstance().getAccountService().getWatchVideoInfo();
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_MINE_GET_WATCH_VIDEO_INFO);
    }

    /**
     * 获取更换银行卡验证码
     */
    public void getBankCardMsg(String phone, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("phone", phone);
        Observable observable = NetLayer.getInstance().getAccountService().getBankCardMsg(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_BANK_UPDATE_CODE);
    }


    /**
     * 修改提醒状态
     */
    public void bankCardUpdate(String bankCardNo, String code, Observer observer) {
        JSONObject json = new JSONObject();
        json.put("bankCardNo", bankCardNo);
        json.put("code", code);
        Observable observable = NetLayer.getInstance().getAccountService().bankCardUpdate(getRequestBody(json));
        toSubscribe2Filter(observable, observer, AccountUrlConstant.API_BANK_UPDATE);
    }

}
